<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择订单类型" clearable>
          <el-option
            v-for="dict in dict.type.order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="下单时间" prop="orderCreateTime">
        <el-date-picker clearable
                        v-model="queryParams.orderCreateTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择下单时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="入库状态" prop="storeStatus">
        <el-select v-model="queryParams.storeStatus" placeholder="请选择入库状态" clearable>
          <el-option
            v-for="dict in dict.type.store_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入库时间" prop="storeTime">
        <el-date-picker clearable
                        v-model="queryParams.storeTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择入库时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="打包状态" prop="packStatus">
        <el-select v-model="queryParams.packStatus" placeholder="请选择打包状态" clearable>
          <el-option
            v-for="dict in dict.type.pack_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="打包时间" prop="packTime">
        <el-date-picker clearable
                        v-model="queryParams.packTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择打包时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="出库状态" prop="outboundStatus">
        <el-select v-model="queryParams.outboundStatus" placeholder="请选择出库状态" clearable>
          <el-option
            v-for="dict in dict.type.outbound_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出库时间" prop="outboundTime">
        <el-date-picker clearable
                        v-model="queryParams.outboundTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择出库时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="盘点状态" prop="stocktakingStatus">
        <el-select v-model="queryParams.stocktakingStatus" placeholder="请选择盘点状态" clearable>
          <el-option
            v-for="dict in dict.type.stocktaking_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="盘点时间" prop="stocktakingTime">
        <el-date-picker clearable
                        v-model="queryParams.stocktakingTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择盘点时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="申请寄件" prop="sendStatus">
        <el-select v-model="queryParams.sendStatus" placeholder="请选择申请寄件" clearable>
          <el-option
            v-for="dict in dict.type.send_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="寄件时间" prop="sendTime">
        <el-date-picker clearable
                        v-model="queryParams.sendTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择寄件时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单id" align="center" prop="id" />
      <el-table-column label="订单编号" align="center" prop="orderNumber" />
      <el-table-column label="订单类型" align="center" prop="orderType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_type" :value="scope.row.orderType"/>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="orderCreateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderCreateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入库状态" align="center" prop="storeStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.store_status" :value="scope.row.storeStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="入库时间" align="center" prop="storeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.storeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="打包状态" align="center" prop="packStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pack_status" :value="scope.row.packStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="打包时间" align="center" prop="packTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.packTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出库状态" align="center" prop="outboundStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.outbound_status" :value="scope.row.outboundStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="出库时间" align="center" prop="outboundTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.outboundTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="盘点状态" align="center" prop="stocktakingStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.stocktaking_status" :value="scope.row.stocktakingStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="盘点时间" align="center" prop="stocktakingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stocktakingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请寄件" align="center" prop="sendStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.send_status" :value="scope.row.sendStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="寄件时间" align="center" prop="sendTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名称" align="center" prop="userName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单操作对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <div class="dynamic-form">
        <!-- 表单切换内容 -->
        <transition name="el-fade-in" mode="out-in">
          <!-- 基本信息模块 -->
          <el-form
            v-if="activeTab === 'basic'"
            key="basic"
            :model="form"
            label-width="100px"
            class="form-section"
            :rules="rules"
            ref="basicForm"
          >
            <el-form-item label="订单编号" prop="orderNumber">
              <el-input v-model="form.orderNumber" placeholder="请输入订单编号" style="width: 217px"/>
            </el-form-item>
            <el-form-item label="订单类型" prop="orderType">
              <el-select v-model="form.orderType" placeholder="请选择">
                <el-option
                  v-for="dict in dict.type.order_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="用户名称" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入用户名称" style="width: 217px"/>
            </el-form-item>
            <el-form-item label="下单时间" prop="orderCreateTime">
              <el-date-picker
                v-model="form.orderCreateTime"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择下单时间"
                style="width: 217px"
              />
            </el-form-item>
            <el-form-item label="订单状态">
              <el-radio-group v-model="form.orderStatus">
                <el-radio
                  v-for="dict in dict.type.order_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"

                >{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>

          <!-- 状态信息模块 -->
          <el-form
            v-else
            key="status"
            :model="form"
            label-width="100px"
            class="form-section"
            :rules="rules"
            ref="statusForm"
          >
            <el-form-item label="订单状态">
              <el-radio-group v-model="form.orderStatus">
                <el-radio
                  v-for="dict in dict.type.order_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"

                >{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="入库状态" style="margin-top: 8px">
              <el-radio-group v-model="form.storeStatus">
                <el-radio
                  v-for="dict in dict.type.store_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="入库时间">
              <el-date-picker
                v-model="form.storeTime"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择入库时间"
              />
            </el-form-item>
            <el-form-item label="打包状态">
              <el-radio-group v-model="form.packStatus">
                <el-radio
                  v-for="dict in dict.type.pack_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="打包时间">
              <el-date-picker
                v-model="form.packTime"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择打包时间"
              />
            </el-form-item>

            <el-form-item label="出库状态">
              <el-radio-group v-model="form.outboundStatus">
                <el-radio
                  v-for="dict in dict.type.outbound_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="出库时间">
              <el-date-picker
                v-model="form.outboundTime"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择出库时间"
              />
            </el-form-item>

            <el-form-item label="盘点状态">
              <el-radio-group v-model="form.stocktakingStatus">
                <el-radio
                  v-for="dict in dict.type.stocktaking_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="盘点时间">
              <el-date-picker
                v-model="form.stocktakingTime"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择盘点时间"
              />
            </el-form-item>

            <el-form-item label="申请寄件">
              <el-radio-group v-model="form.sendStatus">
                <el-radio
                  v-for="dict in dict.type.send_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="寄件时间">
              <el-date-picker
                v-model="form.sendTime"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择寄件时间"
              />
            </el-form-item>
          </el-form>
        </transition>

        <!-- 切换按钮组 -->
        <div class="button-group">
          <el-button
            :type="activeTab === 'basic' ? 'primary' : 'info'"
            @click="activeTab = 'basic'"
          >订单基本信息</el-button>
          <el-button
            :type="activeTab === 'status' ? 'primary' : 'info'"
            @click="activeTab = 'status'"
          >订单状态信息</el-button>
        </div>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/system/info";

export default {
  name: "Info",
  dicts: ['order_status', 'pack_status', 'send_status', 'order_type', 'outbound_status', 'store_status', 'stocktaking_status'],
  data() {
    return {
      activeTab: 'basic', // 当前激活的标签页
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单操作表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNumber: null,
        orderType: null,
        orderStatus: null,
        orderCreateTime: null,
        storeStatus: null,
        storeTime: null,
        packStatus: null,
        packTime: null,
        outboundStatus: null,
        outboundTime: null,
        stocktakingStatus: null,
        stocktakingTime: null,
        sendStatus: null,
        sendTime: null,
        userName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNumber: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
        orderType: [
          { required: true, message: "订单类型不能为空", trigger: "change" }
        ],
        orderStatus: [
          { required: true, message: "订单状态不能为空", trigger: "blur" }
        ],
        orderCreateTime: [
          { required: true, message: "下单时间不能为空", trigger: "blur" }
        ],
        storeStatus: [
          { required: true, message: "入库状态不能为空", trigger: "blur" }
        ],
        packStatus: [
          { required: true, message: "打包状态不能为空", trigger: "blur" }
        ],
        outboundStatus: [
          { required: true, message: "出库状态不能为空", trigger: "blur" }
        ],
        stocktakingStatus: [
          { required: true, message: "盘点状态不能为空", trigger: "blur" }
        ],
        sendStatus: [
          { required: true, message: "申请寄件不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "用户名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单操作列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNumber: null,
        orderType: null,
        orderStatus: 1,
        orderCreateTime: null,
        storeStatus: 2,
        storeTime: null,
        packStatus: 1,
        packTime: null,
        outboundStatus: 1,
        outboundTime: null,
        stocktakingStatus: 1,
        stocktakingTime: null,
        sendStatus: 1,
        sendTime: null,
        userName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单操作";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单操作";
      });
    },
    /** 提交按钮 */
    submitForm() {
      // 根据当前标签获取对应的表单引用
      const formRef = this.activeTab === 'basic' ? 'basicForm' : 'statusForm';
      this.$refs[formRef].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单操作编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    },
    toggleTab() {
      this.activeTab = this.activeTab === 'basic' ? 'status' : 'basic'
    }
  }
};
</script>

<style scoped>
.dynamic-form {
  position: relative;
  min-height: 320px;
}

.form-section {
  padding: 20px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  margin-bottom: 20px;
}

.button-group {
  text-align: left;
  margin-top: 20px;
}

.full-width {
  width: 100%;
}

</style>
